<template>
  <div class="_main">
    <div class="_log_name">Amount</div>
    <div class="_log_count">
      <div @click="onMius">-</div>
      <div>{{ num }}</div>
      <div @click="onPlus">+</div>
      <div class="max" @click="num = props.item.count">Max</div>
      <div>Available:{{ props.item.count }}</div>
    </div>
    <div class="_log_input">
      <div class="_log_input_text">Set Fixed Price</div>
      <div class="_log_sel">
        <el-input
          @input="validateInput"
          class="_log_input_in"
          v-model="input"
          placeholder="Please input"
        />
        <div @click="_activeSelect" class="select_main">
          <span>{{ sleValue }}</span>
          <i ref="selectI"></i>
        </div>
      </div>
    </div>
    <div class="_log_price">
      <div>Total price</div>
      <div>&nbsp;{{ input * num }} {{ sleValue }}</div>
    </div>
    <div class="_log_price">
      <div>Fee</div>
      <div>&nbsp;{{ (input * num * fee).toFixed(2) }} {{ sleValue }}</div>
      <!--      <el-tooltip placement="top" effect="light">-->
      <!--        <template #content> multiple lines<br/>second line</template>-->
      <!--        <div>?</div>-->
      <!--      </el-tooltip>-->
    </div>
    <div class="_log_btn">
      <div @click="props.close">Cancel</div>
      <div @click="submit">{{ props.allow ? "Sell" : "Approve" }}</div>
    </div>
    <div class="select_box" v-show="isChec" ref="selectBox">
      <div
        @click="
          () => {
            sleValue = v;
            _activeSelect();
          }
        "
        :class="sleValue == v ? '_active_sel_list' : ''"
        class="select_list"
        v-for="(v, k) in map"
      >
        {{ v }}
      </div>
    </div>
  </div>
</template>

<script setup>
import { defineProps, ref } from "vue";
import { feeRate, burnRate, typeRate } from "../../../dapp";
const props = defineProps({
  item: {
    type: Object,
  },
  submit: {
    type: Function,
  },
  close: {
    type: Function,
  },
  allow: {
    type: Boolean,
  },
});

function validateInput(value) {
  console.log("input==", value, sleValue.value);
  if (isNaN(value)) {
    value = null;
  } else if (value.indexOf(".") > 0) {
    var w = sleValue.value === "BNB" ? 8 : 0; // BNB保留8位，其他0位小数
    value = value.slice(0, value.indexOf(".") + w);
  }
  input.value = value;
  // return value
}

function submit() {
  // console.log('==', input, props)
  if (input.value > 0) {
    props.submit(input.value, sleValue.value, num.value);
  }
}

function onPlus() {
  if (num.value < props.item.count) {
    num.value = num.value * 1 + 1;
  }
}

function onMius() {
  if (num.value > 1) {
    num.value = num.value * 1 - 1;
  }
}

const fee = ref(0);
const num = ref("1");
const input = ref("");
// let map=['BNB', 'MGC', 'CRA'];
// let sleValue = ref('BNB')
// let map=['MGC', 'CRA'];
let map = ref(["CRA"]);
let sleValue = ref("CRA");
let isChec = ref(false);
const selectI = ref(null);
const _activeSelect = () => {
  isChec.value = !isChec.value;
  selectI.value.className = isChec.value ? "raX" : "";
};

async function init() {
  if (props.item.type === 1) {
    map.value = ["MGC"];
    sleValue.value = "MGC";

    var r = await typeRate(props.item.type);
    var f = r.feeRate;
    var f2 = r.burnRate;
    console.log("fee2=", f, f2);
    fee.value = (f * 1 + f2 * 1) / 1000;
    return;
  }
  var f = await feeRate(props.item.type);
  var f2 = await burnRate(props.item.type);
  console.log("fee=", f, f2);
  fee.value = (f * 1 + f2 * 1) / 1000;
}

init();
</script>

<style scoped lang="scss">
.max {
  cursor: pointer;
}
._main {
  padding: 26px;
}

._log_name {
  font-size: 16px;
  font-family: PingFangSC-Semibold, PingFang SC;
  font-weight: 600;
  color: #2b2b2b;
}

._log_count {
  display: flex;
  align-items: center;
  margin-top: 11px;

  div {
    &:nth-child(1) {
      width: 24px;
      height: 24px;
      background: #ffffff;
      border-radius: 1px;
      border: 1px solid #c4c4c4;
      text-align: center;
      line-height: 0.9;
      font-size: 20px;
      cursor: pointer;
    }

    &:nth-child(2) {
      width: 120px;
      font-size: 20px;
      font-family: PingFangSC-Semibold, PingFang SC;
      font-weight: 600;
      color: #fcc33c;
      text-align: center;
    }

    &:nth-child(3) {
      width: 24px;
      height: 24px;
      background: #ffffff;
      border-radius: 1px;
      border: 1px solid #c4c4c4;
      text-align: center;
      line-height: 0.9;
      font-size: 20px;
      cursor: pointer;
    }

    &:nth-child(4) {
      width: 48px;
      height: 22px;
      background: #ffffff;
      border-radius: 1px;
      border: 1px solid #c4c4c4;
      font-size: 14px;
      font-family: PingFangSC-Semibold, PingFang SC;
      font-weight: 600;
      color: #2b2b2b;
      line-height: 22px;
      text-align: center;
      margin-right: 17px;
      margin-left: 25px;
    }

    &:nth-child(5) {
      font-size: 16px;
      font-family: PingFangSC-Semibold, PingFang SC;
      font-weight: 600;
      color: #696969;
    }
  }
}

._log_input {
  margin-top: 28px;
  margin-bottom: 6px;
  overflow: hidden;
}

._log_input_text {
  font-size: 16px;
  font-family: PingFangSC-Semibold, PingFang SC;
  font-weight: 600;
  color: #2b2b2b;
}

:deep(.el-input__inner) {
  height: 47px;
  margin-top: 8px;
  text-align: right;
}

._log_price {
  display: flex;
  align-items: center;
  margin-top: 12px;

  div {
    &:nth-child(1) {
      font-size: 14px;
      font-family: PingFangSC-Semibold, PingFang SC;
      font-weight: 600;
      color: #696969;
      width: 72px;
      white-space: nowrap;
    }

    &:nth-child(2) {
      font-size: 14px;
      font-family: PingFangSC-Semibold, PingFang SC;
      font-weight: 600;
      color: #2b2b2b;
      margin-left: 15px;
    }

    &:nth-child(3) {
      width: 16px;
      height: 16px;
      background: #d7d7d7;
      border-radius: 50%;
      color: #fff;
      font-size: 12px;
      text-align: center;
      line-height: 16px;
      margin-left: 6px;
    }
  }
}

._log_btn {
  display: flex;
  align-items: center;
  margin-top: 29px;
  justify-content: space-between;

  div {
    width: 206px;
    /*width: 156px;*/
    height: 45px;
    background: #fcc33c;
    font-size: 14px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #383838;
    line-height: 45px;
    text-align: center;
    cursor: pointer;
  }
}

@media screen and (max-width: 750px) {
  ._log_btn {
    div {
      width: 140px;
    }
  }
}

._log_sel {
  padding-right: 64px;
  border: 1px solid #c4c4c4;
  height: 47px;
  overflow: hidden;
  position: relative;
  display: flex;
  align-items: center;
}

:deep(.el-input__inner) {
  border: none;
  margin-top: 0 !important;
}

.select_main {
  /*height: 100%;*/
  width: 74px;
  background: transparent;
  /*position: absolute;*/
  color: #2b2b2b;
  opacity: 1;
  padding-right: 10px;
  position: relative;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  span {
    width: 0;
    flex-grow: 1;
  }
  i {
    width: 16px;
    height: 16px;
    display: inline-block;
    background: url("@/assets/xl.png") no-repeat center;
    background-size: cover;
    transform: rotateZ(180deg);
    transform-origin: center;
    transition: all 0.2s linear;
    /*position: absolute;*/
    /*top: 50%;*/
    /*<!--transform: translateX(-50%);-->*/
  }
}

.select_box {
  width: 76px;
  position: absolute;
  right: 27px;
  top: 240px;
  background: #fff;
  border: 1px solid #d9d9d9;
  overflow: hidden;
  transition: all 1s linear;
}

.select_list {
  width: 100%;
  height: 40px;
  /*border-bottom: 1px solid rgba(216, 188, 132, .6);*/
  font-size: 14px;
  font-weight: 400;
  color: #afafaf;
  line-height: 40px;
  text-align: center;
  cursor: pointer;
}
._active_sel_list {
  background-color: #fde6ad;
}
.raX {
  transform: rotateZ(0deg) !important;
}
</style>
